package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.Activity;
import com.wuming.house.model.Market;
import com.wuming.house.service.ActivityService;
import com.wuming.house.service.MarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/8/27 18:51
 * @Description:
 */
@Controller
@RequestMapping("/web/market")
public class MarketController extends BaseController {

    @Autowired
    private MarketService marketService;

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) String title, @RequestParam(required = false) String source,
                             @RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "10") int pageSize) {
        logger.info("准备查询所有行情");
        try {
            List<Market> markets = marketService.getMarketsByPage(title, source, pageIndex, pageSize);
            int total = marketService.getTotal(title, source);
            return CommonResult.createSuccessResult(markets, total, "查询所有行情成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
        }
        return CommonResult.createFailureResult("查询所有行情失败");
    }

    @RequestMapping("/toManageMarket")
    public String toManageMarket() {
        return "/tools/manageMarket";
    }

    @RequestMapping("/toEditMarket/{type}/{id}")
    public String toEditMarket(@PathVariable Integer type, @PathVariable Integer id, Model model) {
        if (type == 2) {
            if (null != id) {
                model.addAttribute("market", marketService.getMarketById(id));
            }
        }
        return "/tools/editMarket";
    }

    @ResponseBody
    @RequestMapping(value = "/edit")
    public JsonResult edit(Market market) {
        if (null == market.getId()) {
            market.setCreateDatetime(new Date());
            marketService.saveMarket(market);
            return new JsonResult(null, "添加行情成功");
        } else {
            marketService.modifyMarket(market);
            return new JsonResult(null, "更新行情成功");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Integer id) {
        logger.info("准备删除id:{}的行情", id);
        try {
            marketService.delete(id);
            return new JsonResult(null, "删除成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
            return new JsonResult(null, "删除失败", false);
        }
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file, HttpServletRequest request) {
        try {
            String path = this.uploadImage(file, "market", 1);
            logger.info("文件成功上传到指定目录下,文件目录:{},", path);
            return new JsonResult(path, "上传成功", true);
        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult("", "上传失败", false);
    }

    @ResponseBody
    @RequestMapping("/get/{marketId}")
    public JsonResult get(@PathVariable Integer marketId) {
        try {
            Market market = marketService.getMarketById(marketId);
            return new JsonResult(market.getUrl(), "", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(null, "", false);
    }

}
